import java.util.Vector;

import sdljava.SDLException;
import sdljava.video.*;
import sdljava.x.swig.SWIG_SDLVideoConstants;
import sdljavax.gfx.SDLGfx;


public class Sprite {
	protected GameWindow window;
	protected Vector<SDLRect> spriteImage;
	protected SDLSurface spriteImageFull;
	protected String spriteImageFile;
	protected ImageData spriteImageData;
	protected Integer positionX;
	protected Integer positionY;
	private static int vertical = 0;
	private static int horizontal = 1;
	
	// This will hold data that relates to the sprite
	// The positionX and positionY here are the position of the top left corner of the sprite on the window
	// the spriteImage vector holds all the clips of the sprite (eg. different pictures of start button, mouse over, mouse off)
	public Sprite(GameWindow gameWindow, ImageData imageData, int x, int y, String imageFile) throws SDLException{
		this.window = gameWindow;
		this.positionX = x;
		this.positionY = y;
		this.spriteImage = new Vector<SDLRect>();
		this.spriteImageData = imageData;
		this.spriteImageFile = imageFile;
		this.spriteImageFull = loadfile(this.spriteImageFile);
		if (spriteImageData.amount>0){
			clip();
		}
	}
	
	private void clip(){
		spriteImage.clear();
		int cornerx = (int) (spriteImageData.cornerx*spriteImageData.zoomx);
		int cornery = (int) (spriteImageData.cornery*spriteImageData.zoomy);
		int amount = spriteImageData.amount;
		int orientation = spriteImageData.orientation;
		SDLRect button;
		for(int i=0; i<amount; i++){
			button = new SDLRect();
			if(orientation == horizontal){
				button.x = cornerx;
				button.y = cornery;
				button.width = doubleToInt(spriteImageData.width*spriteImageData.zoomx);
				button.height = doubleToInt(spriteImageData.height*spriteImageData.zoomy);
				getSpriteImage().add(button);
				cornerx += doubleToInt(spriteImageData.width*spriteImageData.zoomx);
			}
			else if(orientation == vertical){
				button.x = cornerx;
				button.y = cornery;
				button.width = doubleToInt(spriteImageData.width*spriteImageData.zoomx);
				button.height = doubleToInt(spriteImageData.height*spriteImageData.zoomy);
				getSpriteImage().add(button);
				cornery += doubleToInt(spriteImageData.height*spriteImageData.zoomy);
			}
		}
	}
	private SDLSurface loadfile(String filename) throws SDLException{
		SDLSurface loadedImage = null;
		loadedImage = SDLVideo.loadBMP(filename);
		SDLSurface optimizedImage = loadedImage.displayFormat();
		optimizedImage = SDLGfx.zoomSurface(optimizedImage, spriteImageData.zoomx, spriteImageData.zoomy, false);
		long colorkey = SDLVideo.mapRGB(optimizedImage.getFormat(), 0xFF, 0xFF, 0x00);
		optimizedImage.setColorKey(SWIG_SDLVideoConstants.SDL_SRCCOLORKEY, colorkey);
		loadedImage.freeSurface();
		return optimizedImage;
	}
	public void show(SDLRect clip) throws SDLException{
		SDLRect offset = new SDLRect();
		
		offset.setX(positionX);
		offset.setY(positionY);
		
		spriteImageFull.blitSurface(clip, window.getGameSurface(), offset);
		window.getGameSurface().flip();
	}
	
	public void reZoom() throws SDLException{
		spriteImageFull = loadfile(this.spriteImageFile);
		clip();
	}
	
	private int doubleToInt(double changeMe){
		if((int)changeMe != (int)(changeMe+0.5)){
			return ((int)changeMe + 1);
		}
		return (int)changeMe;
	}

	public SDLSurface getSpriteImageFull() {
		return spriteImageFull;
	}

	public Integer getPositionX() {
		return positionX;
	}

	public Integer getPositionY() {
		return positionY;
	}

	public Vector<SDLRect> getSpriteImage() {
		return spriteImage;
	}

	public ImageData getSpriteImageData() {
		return spriteImageData;
	}
}
